package com.doumi.biz.qadev.api.util;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public final class DateUtil {
	private DateUtil() {
	}

	public static final int DAY = 1;
	public static final int HOUR = 2;
	public static final int MINITE = 3;
	public static final int SECOND = 4;
	public static final int MONTH = 5;

	public static final int DAY_SECOND = 24 * 60 * 60;

	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String DEFAULT_DATE_FORMAT1 = "yyyyMMddHHmmssSSSS";
	public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
	public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
	private static final DateFormat date_format = new SimpleDateFormat("yyyy/M/d", Locale.ENGLISH);
	private static final DateFormat time_format = new SimpleDateFormat("yyyy/M/d hh:mm:ss", Locale.ENGLISH);
	private static final DateFormat time_format1 = new SimpleDateFormat(DEFAULT_DATE_FORMAT1, Locale.ENGLISH);

	public static String defFormatTime1(Date date) {
		return time_format1.format(date);
	}

	public static String defFormatDate(Date date) {
		return date_format.format(date);
	}

	public static String defFormatTime(Date date) {
		return time_format.format(date);
	}

	public static String dateFormat(Date date) {
		return dateFormat(date, DEFAULT_DATE_FORMAT);
	}

	public static String getYestdayYYYYMMDD() {
		LocalDate localDate = LocalDate.now();
		int year = localDate.getYear();
		int month = localDate.getMonthValue();
		int day = localDate.getDayOfMonth();
		Calendar cal = getInstance();
		cal.set(year, month - 1, day - 1);// 昨天，月是从0开始，所以要-1
		return dateFormat(cal.getTime(), DATE_FORMAT_YYYYMMDD);
	}
	public static String getYestdayYYYY_MM_DD() {
		LocalDate localDate = LocalDate.now();
		int year = localDate.getYear();
		int month = localDate.getMonthValue();
		int day = localDate.getDayOfMonth();
		Calendar cal = getInstance();
		cal.set(year, month - 1, day -1);// 昨天，月是从0开始，所以要-1
		return dateFormat(cal.getTime(), DATE_FORMAT_YYYY_MM_DD);
	}

	public static String dateFormat(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String str = sdf.format(date);
		return str;
	}

	public static Date dateFormat(String dateStr) {
		return dateFormat(dateStr, DEFAULT_DATE_FORMAT);
	}

	public static Date dateFormat(String dateStr, String format) {
		if (StringUtils.isBlank(dateStr)) {
			return null;
		}
		DateFormat dateFormat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = dateFormat.parse(dateStr);
		} catch (ParseException e) {
			return null;
		}

		return date;
	}

	/**
	 * 获取延迟时间
	 * 
	 * @return
	 */
	public static long getDelaytime(int delayhour) {
		Calendar cal = getInstance();
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);
		int millsecond = cal.get(Calendar.MILLISECOND);

		long delta = (delayhour - hour) * 3600000 - minute * 60000 - second * 1000 - millsecond;
		if (delta < 0) {
			delta += 86400000;
		}
		return delta;
	}

	/**
	 * 获取延迟时间
	 * 
	 * @return
	 */
	public static long getWeekDelaytime(int delayhour) {
		Calendar cal = getInstance();
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);
		int millsecond = cal.get(Calendar.MILLISECOND);

		long delta = (delayhour - hour) * 3600000 - minute * 60000 - second * 1000 - millsecond;
		int weekDay = getWeekDay();

		int day = 7 - weekDay + 1;

		if (delta < 0) {
			delta += 86400000 * day;
		}
		return delta;
	}

	/**
	 * 获取下次刷新时间
	 * 
	 * @return
	 */
	public static long getNextTime(int delayhour) {
		Calendar cal = getInstance();
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);
		int millsecond = cal.get(Calendar.MILLISECOND);

		long delta = (delayhour - hour) * 3600000 - minute * 60000 - second * 1000 - millsecond;
		if (delta < 0) {
			delta += 86400000;
		}
		return getMillis() + delta;
	}

	// 是否同一天
	public static boolean isTheSameDay(Date d1, Date d2) {
		Calendar c1 = getInstance();
		Calendar c2 = getInstance();
		c1.setTime(d1);
		c2.setTime(d2);

		return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
				&& (c1.get(Calendar.DATE) == c2.get(Calendar.DATE));
	}

	public static boolean isToday(Date lastDate) {
		Calendar c1 = getInstance();
		Calendar c2 = getInstance();
		c1.setTime(lastDate);
		return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
				&& (c1.get(Calendar.DATE) == c2.get(Calendar.DATE));
	}

	public static boolean isYesterday(Date lastDate) {
		return isYesterday(lastDate, currentDate());
	}

	public static boolean isYesterday(Date lastDate, Date date) {
		Calendar last = getInstance();
		Calendar now = getInstance();
		last.setTime(lastDate);
		now.setTime(date);
		last.add(Calendar.DAY_OF_MONTH, 1);
		return (last.get(Calendar.YEAR) == now.get(Calendar.YEAR)) && (last.get(Calendar.MONTH) == now.get(Calendar.MONTH))
				&& (last.get(Calendar.DATE) == now.get(Calendar.DATE));
	}

	public static Date getZeroTime() {
		return getBeginTime(0);
	}

	public static Date getZeroTime(Date date) {
		return getBeginTime(date, 0);
	}

	public static Date getBeginTime(int hour) {
		Calendar begin = getInstance();

		begin.set(Calendar.HOUR_OF_DAY, hour);
		begin.set(Calendar.MINUTE, 0);
		begin.set(Calendar.SECOND, 0);
		begin.set(Calendar.MILLISECOND, 0);
		return begin.getTime();
	}

	public static Date getBeginTime(Date date, int hour) {
		Calendar begin = getInstance();
		begin.setTime(date);
		begin.set(Calendar.HOUR_OF_DAY, hour);
		begin.set(Calendar.MINUTE, 0);
		begin.set(Calendar.SECOND, 0);
		begin.set(Calendar.MILLISECOND, 0);
		return begin.getTime();
	}

	public static Date getEndTime() {
		Calendar begin = getInstance();
		begin.set(Calendar.HOUR_OF_DAY, 23);
		begin.set(Calendar.MINUTE, 59);
		begin.set(Calendar.SECOND, 59);
		begin.set(Calendar.MILLISECOND, 999);
		return begin.getTime();
	}

	public static Date getEndTime(Date date) {
		Calendar begin = getInstance();
		begin.setTime(date);
		begin.set(Calendar.HOUR_OF_DAY, 23);
		begin.set(Calendar.MINUTE, 59);
		begin.set(Calendar.SECOND, 59);
		begin.set(Calendar.MILLISECOND, 999);
		return begin.getTime();
	}

	public static boolean isExpired(Date last, int coolSecond) {
		if (null == last || coolSecond <= 0) {
			return true;
		}
		Date coolPassDate = addTime(SECOND, coolSecond, last);

		return coolPassDate.before(currentDate());
	}

	public static boolean isExpired(Date last) {
		if (null == last) {
			return true;
		}
		Date now = currentDate();

		return last.before(now);
	}

	public static Date addTime(int param, int add) {
		return addTime(param, add, currentDate());
	}

	public static Date addTime(int param, int add, Date date) {
		if (null == date) {
			return null;
		}
		Calendar begin = getInstance();
		begin.setTime(date);

		if (DAY == param)
			begin.add(Calendar.DATE, add);
		if (HOUR == param)
			begin.add(Calendar.HOUR_OF_DAY, add);
		if (MINITE == param)
			begin.add(Calendar.MINUTE, add);
		if (SECOND == param)
			begin.add(Calendar.SECOND, add);
		if (MONTH == param) {
			begin.add(Calendar.MONTH, add);
		}

		return begin.getTime();
	}

	public static long getSecondsToGoFromNow(Date fromDate) {
		if (null == fromDate) {
			return -1L;
		}
		Date now = currentDate();
		if (now.before(fromDate)) {
			return 0L;
		}
		return (now.getTime() - fromDate.getTime()) / 1000L;
	}

	public static long getSecondsToGo(Date fromDate, Date toDate) {
		if ((null == fromDate) || (null == toDate)) {
			return -1L;
		}
		if (toDate.before(fromDate)) {
			return 0L;
		}
		return (toDate.getTime() - fromDate.getTime()) / 1000L;
	}

	public static int getSeconds() {
		return (int) (getMillis() / 1000L);
	}

	public static long getMillis() {
		return currentDate().getTime();
	}

	public static int getWeekDay() {
		Calendar cal = getInstance();

		int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (week == 0)
			week = 7;
		return week;
	}

	public static int getHourOfDay() {
		Calendar cal = getInstance();
		return cal.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMonthDay() {
		Calendar cal = getInstance();

		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 
	 * @Title: currentDate @Description: 获得当前时间 @param @return 参数 @return Date
	 *         返回类型 @throws
	 */
	public static Date currentDate() {
		Calendar cal = getInstance();
		return cal.getTime();
	}

	public static Calendar getInstance() {
		Calendar cal = Calendar.getInstance();
		cal.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		return cal;
	}

	// 根据年月日计算年龄,birthTimeString:"1994-11-14"
	public static int getAgeFromBirthTime(Date birthDay) {
		if (birthDay == null) {
			return 21;
		}
		// 先截取到字符串中的年、月、日
		Calendar birthCal = getInstance();
		birthCal.setTime(birthDay);

		int selectYear = birthCal.get(Calendar.YEAR);
		int selectMonth = birthCal.get(Calendar.MONTH) + 1;
		int selectDay = birthCal.get(Calendar.DAY_OF_MONTH);

		// 得到当前时间的年、月、日
		Calendar cal = getInstance();
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;
		int dayNow = cal.get(Calendar.DATE);

		// 用当前年月日减去生日年月日
		int yearMinus = yearNow - selectYear;
		int monthMinus = monthNow - selectMonth;
		int dayMinus = dayNow - selectDay;

		int age = yearMinus;// 先大致赋值
		if (yearMinus < 0) {// 选了未来的年份
			age = 0;
		} else if (yearMinus == 0) {// 同年的，要么为1，要么为0
			if (monthMinus < 0) {// 选了未来的月份
				age = 0;
			} else if (monthMinus == 0) {// 同月份的
				if (dayMinus < 0) {// 选了未来的日期
					age = 0;
				} else if (dayMinus >= 0) {
					age = 1;
				}
			} else if (monthMinus > 0) {
				age = 1;
			}
		} else if (yearMinus > 0) {
			if (monthMinus < 0) {// 当前月>生日月
			} else if (monthMinus == 0) {// 同月份的，再根据日期计算年龄
				if (dayMinus < 0) {
				} else if (dayMinus >= 0) {
					age = age + 1;
				}
			} else if (monthMinus > 0) {
				age = age + 1;
			}
		}
		return age;
	}

	/**
	 * 说明：只获取 年月日
	 */
	public static Date getYYYYMMDD(Date date) {
		if (date == null) {
			return null;
		}
		String dateStr = dateFormat(date, DATE_FORMAT_YYYYMMDD);
		return dateFormat(dateStr, DATE_FORMAT_YYYYMMDD);
	}
}
